#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <error.h>

void * thread_func(void *arg)
{
    /* 线程分离 */
    pthread_detach(pthread_self());

    printf("new thread:%ld\n", pthread_self());

    while (1)
    {
        printf("I am thread...\n");
        sleep(1);
    }
    
    /* 线程退出 */
    //int * retStatus =(int *)malloc(sizeof(int) * 1);
    //*ret = -45;
    static int retStatus = -45;//加 static 防止栈空间里的值被释放
    pthread_exit(&retStatus);
}

int main()
{
    /* 线程的创建 */
    pthread_t tid;
    int ret = pthread_create(&tid, NULL, thread_func, NULL);
    if (ret != 0)
    {
        printf("pthread_create rror\n");
        exit(-1);
    }
    

    /* 这边的延时操作是为了让子线程先执行先分离掉 */
    sleep(1);
    
    /* 阻塞等待回收 */
    /* 回收子线程的资源 */
    int * retStatus = NULL;
    ret = pthread_join(tid, (void **)&retStatus);
    printf("ret = %d\n", ret);
    if (ret == EINVAL)
    {
        printf("detached...\n");
    }

    int cnt = 5;
    while (cnt--)
    {
        printf("main thread\n");
        sleep(1);
    }
    if (retStatus)
    {
        /* 解引用 */
        printf("retStatus:%d\n", *retStatus);
    }    

    return 0;
}